/**   
* @Title: HttpDownloader.java
* @Package me.crawler.downloader
* @Description: TODO
* @author xaoyaoyao
* @date Jan 15, 2019 3:07:26 PM
* @version V1.0   
*/
package me.crawler.downloader;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;

import lombok.Builder;
import lombok.extern.slf4j.Slf4j;
import me.crawler.entity.SpiderTask;
import me.crawler.enumerate.RequestMethodEnum;
import me.crawler.util.HttpUtil;

/**
 * @ClassName: HttpDownloader
 * @Description: TODO
 * @author xaoyaoyao
 * @date Jan 15, 2019 3:07:26 PM
 * 
 */
@Slf4j
@Builder
public class HttpDownloader {

	/** 任务 **/
	private SpiderTask task;

	/** cookie **/
	private String cookie;

	/** ua **/
	private String ua;

	@SuppressWarnings("unchecked")
	private Map<String, String> init(String headers) {
		Map<String, String> headerParams = null;
		if (StringUtils.isNoneBlank(headers)) {
			try {
				headerParams = JSON.parseObject(headers.toString(), Map.class);
			} catch (Exception e) {
				log.error(">>>>>>>> headers >> {}", headers);
				log.error(e.getMessage(), e);
			}
		}
		ua = StringUtils.isBlank(ua) ? HttpUtil.DEFAULT_UA : ua;
		if (StringUtils.isNoneBlank(cookie)) {
			if (MapUtils.isEmpty(headerParams)) {
				headerParams = new ConcurrentHashMap<String, String>(1);
			}
			headerParams.put("Cookie", cookie);
		}
		return headerParams;
	}

	/**
	 * @Title: download
	 * @Description: get获取请求结果
	 * @return String
	 */
	@SuppressWarnings("unchecked")
	public String download() {
		log.debug(">>>>>>>> SpiderTask cannot be empty.");
		if (null == task) {
			return null;
		}
		String url = this.task.getUrl(), parameters = task.getParameters(), requestMethod = task.getRequestMethod(), headers = task.getHeaderParams();
		log.debug(">>>>>>>> url >> {}", url);
		log.debug(">>>>>>>> parameters >> {}, requestMethod >> {}, headerParams >> {}", parameters, requestMethod, headers);
		log.debug(">>>>>>>> ua >> {}, cookie >> {}, headerParams >> {}", ua, cookie, headers);
		if (StringUtils.isNoneBlank(url)) {
			Map<String, String> headerParams = init(headers);
			Map<String, Object> params = null;
			try {
				if (StringUtils.isNoneBlank(parameters)) {
					params = JSON.parseObject(parameters.toString(), Map.class);
				}
			} catch (Exception e) {
				log.error(">>>>>>>> parameters >> {}", parameters);
				log.error(e.getMessage(), e);
			}
			String result = null;
			RequestMethodEnum methodEnum = RequestMethodEnum.from(requestMethod);
			if (null != methodEnum && methodEnum.equals(RequestMethodEnum.JSON)) {
				result = HttpUtil.getInstance().postRequestBody(JSON.toJSONString(params), url, null, ua, headerParams);
			} else if (null != methodEnum && methodEnum.equals(RequestMethodEnum.POST)) {
				result = HttpUtil.getInstance().post(url, params, ua, headerParams);
			} else {
				result = HttpUtil.getInstance().get(url, ua, headerParams);
			}
			log.debug(">>>>>>>> result >> {}", result);
			return result;
		}
		return null;
	}
}
